package cn.codeinn.generator.util;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;

import cn.vkits.core.lang.Dict;
import cn.vkits.db.sql.Order;

public class Page<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 查询参数
	 */
	private Dict condition;

	/** 排序 */
	private Order[] orders;

	/**
	 * 查询数据列表
	 */
	private List<T> data;

	/**
	 * 总页数
	 */
	private int pages;

	/**
	 * 总记录数
	 */
	private long total;

	/**
	 * 每页的数量
	 */
	private int pageSize;

	/**
	 * 当前的页数
	 */
	private int pageNum;

	public void setPage(PageInfo<T> info) {
		this.data = info.getList();
		this.total = info.getTotal();
		this.pages = info.getPages();
	}

	public Page(List<T> list) {
		this.data = list;
	}

	public Page(Map<? extends String, ? extends Object> map) {
		this.condition = Dict.create();
		condition.putAll(map);
		if (null != map.get("pageSize")) {
			this.pageNum = condition.getInt("pageNum");
			this.pageSize = condition.getInt("pageSize");
		}
	}

	public T getCondition(Class<T> aClass) {
		return condition.toBean(aClass);
	}

	public Dict getCondition() {
		return condition;
	}

	public void setCondition(Map<? extends String, ? extends Object> map) {
		this.condition = Dict.create();
		condition.putAll(map);
	}

	public Order[] getOrders() {
		return orders;
	}

	public void setOrders(Order[] orders) {
		this.orders = orders;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setCondition(Dict condition) {
		this.condition = condition;
	}

	@Override
	public String toString() {
		return "Page [condition=" + condition + ", orders=" + Arrays.toString(orders) + ", data=" + data + ", pages=" + pages + ", total=" + total + ", pageSize=" + pageSize + ", pageNum=" + pageNum + "]";
	}

}
